package com.jiaorui.text;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.PanelUI;

import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;
import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI;
import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI.NormalColor;
import org.jb2011.lnf.beautyeye.widget.border.BERoundBorder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Main extends JFrame implements ActionListener, KeyListener, ChangeListener {

	private stateHandler state;

	private static PaintButton stateButton;

	private MapCanvas canvas;
	
	private JColorChooser colorChooser;
	
	private Color shipColor;

	private JComboBox<String> combo;
	
	static JTextArea textField = new JTextArea("这是一个接收消息的示例");
	
	Font f=new Font("宋体",Font.BOLD,20);
	
	private int change = 0;
	
	private String DEFAULT_TIME_FORMAT = "HH:mm:ss";
	 
	private String time;
	
	private int ONE_SECOND = 1000;
	
	JTextField s_text=null;

	final JTextField A_text;
	final JTextField R_text;
	final JTextField status_text;
	final JTextField f_text;
	final JTextField E_text;
	 Thread Random_thread ;

	public static void main(String[] args) throws  InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
	    try
	    {
	    	BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;
	        org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
	        BeautyEyeLNFHelper.translucencyAtFrameInactive = false;
	    }
	    catch(Exception e)
	    {
	        //TODO exception
	    }
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					new Main();
				} catch (ClassNotFoundException | InstantiationException
						| IllegalAccessException
						| UnsupportedLookAndFeelException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

	public Main() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
		this.setUndecorated(true);
		this.setExtendedState(JFrame.MAXIMIZED_BOTH); 
		//UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
		state = new stateHandler();
		

		// Window setup
		setLocation(50, 50);
		setSize(1366, 768);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		final Container content = getContentPane();
		content.setLayout(null);
		

		shipColor = content.getBackground();
		
		BERoundBorder beRoundBorder = new BERoundBorder(Color.LIGHT_GRAY, 10);
		beRoundBorder.setArcWidth(20);
		
		
		JLabel gameLabel = new JLabel("经纬仪多区域联合仿真样机 Theodolite multi-area joint simulation prototype");
		gameLabel.setPreferredSize(new Dimension(40,40));
		gameLabel.setBorder(beRoundBorder);
		gameLabel.setSize(1366, 30);
		gameLabel.setLocation(0, 0);
		content.add(gameLabel);


		String[] comboStrings = { "ͨ通视算法模型", "传输效应计算模型"};
		combo = new JComboBox<String>(comboStrings);
		combo.setFont(new Font("宋体",Font.BOLD,15));

		JLabel jl = new JLabel();
		jl.setLocation(0, 0);
		jl.setSize(700, 642);
		jl.setBorder(beRoundBorder);
		URL url = Main.class.getResource("aa.jpg");        
		Icon icon = new ImageIcon(url);       
		jl.setIcon(icon);       
		jl.setOpaque(true);  
		canvas = new MapCanvas();
		canvas.setLayout(null);
		canvas.setLocation(0, 30);
		canvas.setSize(700, 642);
		canvas.setBorder(beRoundBorder);
		canvas.add(jl);
		canvas.setOpaque(true);
		canvas.setVisible(true);
		content.add(canvas);
		
		final int WIDTH=100;
		final int HEIGHT=100;
		final JFXPanel webBrowser = new JFXPanel();
		webBrowser.setVisible(false);
		webBrowser.setLayout(null);
		webBrowser.setLocation(0, 35);
		webBrowser.setSize(700, 642);
		webBrowser.setBorder(beRoundBorder);
		content.add(webBrowser);
		
		Platform.runLater(new Runnable(){

			@Override
			public void run() {
				Double widthDouble = new Integer(WIDTH).doubleValue();  
                Double heightDouble = new Integer(HEIGHT).doubleValue();  

				java.io.File file = new java.io.File("D:\\feiji.mp4"); 
				String url = file.toURI().toString();
				Media media = new Media(url);
				MediaPlayer mplayer = new MediaPlayer(media);
				mplayer.setCycleCount(MediaPlayer.INDEFINITE);
                MediaView mView = new MediaView(mplayer);
                System.out.println(media.getSource());
                
                Pane pane = new Pane();
                pane.getChildren().add(mView);
                mView.fitHeightProperty().bind(pane.heightProperty());
                mView.fitWidthProperty().bind(pane.widthProperty());
                
                
                Scene scene = new Scene(pane, widthDouble, heightDouble);  
                webBrowser.setScene(scene);  
                webBrowser.show();
                mplayer.play();

			}
			
		});
		
		
		
		final JPanel controls = new JPanel();
		controls.setLocation(0, 672);
		controls.setSize(1366,138);
		controls.setBorder(beRoundBorder);
		controls.add(combo);
		
		
		final JLabel swich = new JLabel();
		

		stateButton = new PaintButton("打开激光", canvas, (String) combo.getSelectedItem(), state, this);
		stateButton.setUI(new BEButtonUI().setNormalColor(NormalColor.red));
		controls.add(stateButton);
		ActionListener StateButtonActionListener = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(state.pausedState==1){
					stateButton.setLabel("打开激光");
					stateButton.setUI(new BEButtonUI().setNormalColor(NormalColor.red));
					swich.setIcon(new ImageIcon(Main.class.getResource("off.jpg")));
					canvas.setVisible(true);
					webBrowser.setVisible(false);
				}else{
					stateButton.setLabel("关闭激光");
					stateButton.setUI(new BEButtonUI().setNormalColor(NormalColor.green));
					swich.setIcon(new ImageIcon(Main.class.getResource("on.jpg")));
				}
				state.flipState();
			}
		};
		stateButton.addActionListener(StateButtonActionListener);
		stateButton.setPreferredSize(new Dimension(200,80));
		stateButton.setFont(f);
		
//		PaintButton stateButton2 = new PaintButton("经纬仪跟踪状态", canvas, (String) combo.getSelectedItem(), state, this);
//		stateButton2.setPreferredSize(new Dimension(200,80));
//		stateButton2.setFont(f);
//		controls.add(stateButton2);

		PaintButton resetButton = new PaintButton("获取电视", canvas, (String) combo.getSelectedItem(), state, this);
		resetButton.setPreferredSize(new Dimension(200,80));
		resetButton.setFont(f);
		resetButton.addMouseListener(new MouseAdapter(){
			 public void mouseClicked(MouseEvent e){
				 if(change==0){
					 canvas.setVisible(false);
					 webBrowser.setVisible(true);
				 }else{
					 canvas.setVisible(true);
					 webBrowser.setVisible(false);
				 }
				 change=change==0?1:0;
		     }
		});
		controls.add(resetButton);
		
		
		PaintButton stateButton4 = new PaintButton("关闭", canvas, (String) combo.getSelectedItem(), state, this);
		stateButton4.setPreferredSize(new Dimension(200,80));
		stateButton4.setFont(f);
		controls.add(stateButton4);
		ActionListener StateButton4ActionListener = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
			
		};
		stateButton4.addActionListener(StateButton4ActionListener);
		
		
		
		content.add(controls);

		final JPanel showParam = new JPanel();
		showParam.setPreferredSize(new Dimension(666,420));
		showParam.setLayout(null);
		JLabel param = new JLabel("        经纬仪参数列表");
		JLabel param1 = new JLabel("        经纬仪状态列表");
		
		
		 JLabel s = new JLabel("时间/H:m:s");
		 s.setBounds(325, 320, 180, 30);
		 s.setFont(new Font("宋体", Font.ROMAN_BASELINE, 20));
		 showParam.add(s);
		 s_text = new JTextField();
		 s_text.setFont(new Font("宋体", Font.ROMAN_BASELINE, 20));
		 s_text.setBounds(470, 320, 120, 30);
		 showParam.add(s_text);
		
		
		swich.setBounds(380, 80, 160, 160);
		swich.setIcon(new ImageIcon(Main.class.getResource("off.jpg")));
		swich.setOpaque(true); 
		showParam.add(swich);
		swich.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e){
				if(state.pausedState==0){
					swich.setIcon(new ImageIcon(Main.class.getResource("on.jpg")));
					stateButton.setLabel("关闭激光");
					stateButton.setUI(new BEButtonUI().setNormalColor(NormalColor.green));
				}else{
					swich.setIcon(new ImageIcon(Main.class.getResource("off.jpg")));
					stateButton.setLabel("打开激光");
					stateButton.setUI(new BEButtonUI().setNormalColor(NormalColor.red));
					canvas.setVisible(true);
					 webBrowser.setVisible(false);
				}
				state.flipState();
		      }
		});
		
		
		param1.setBounds(320, 0, 300, 50);
		param.setBounds(0, 0, 300, 50);
		param.setFont(new Font("宋体", Font.ROMAN_BASELINE, 20));
		param1.setFont(new Font("宋体", Font.ROMAN_BASELINE, 20));
		showParam.add(param);
		showParam.add(param1);
		param.setBorder(beRoundBorder);
		param1.setBorder(beRoundBorder);
		
		JLabel A = new JLabel("方位角/A");
		 A.setBounds(70, 80, 80, 30);
		 A.setFont(new Font("宋体", Font.ROMAN_BASELINE, 20));
		 showParam.add(A);
		 A_text = new JTextField();
		 A_text.setBounds(180, 80, 100, 30);
		 A_text.setFont(new Font("宋体", Font.ROMAN_BASELINE, 20));
		 showParam.add(A_text);
		 
		 
		 
		 JLabel R = new JLabel("俯仰角/R");
		 R.setBounds(85, 160, 80, 30);
		 R.setFont(new Font("宋体", Font.ROMAN_BASELINE, 20));
		 showParam.add(R);
		 R_text = new JTextField();
		 R_text.setFont(new Font("宋体", Font.ROMAN_BASELINE, 20));
		 R_text.setBounds(180, 160, 100, 30);
		 showParam.add(R_text);
		 
		 
		 
		 JLabel status = new JLabel("跟踪状态/Status");
		 status.setBounds(1, 240, 170, 30);
		 status.setFont(new Font("宋体", Font.ROMAN_BASELINE, 20));
		 showParam.add(status);
		 status_text = new JTextField();
		 status_text.setFont(new Font("宋体", Font.ROMAN_BASELINE, 20));
		 status_text.setBounds(180, 240, 100, 30);
		 showParam.add(status_text);
		 
		 
		 JLabel E = new JLabel("焦距/f");
		 E.setBounds(75, 320, 80, 30);
		 E.setFont(new Font("宋体", Font.ROMAN_BASELINE, 20));
		 showParam.add(E);
		 E_text = new JTextField();
		 E_text.setFont(new Font("宋体", Font.ROMAN_BASELINE, 20));
		 E_text.setBounds(180, 320, 100, 30);
		 showParam.add(E_text);
		
		 JLabel f = new JLabel("距离/R");
		 f.setBounds(90, 400, 80, 30);
		 f.setFont(new Font("宋体", Font.ROMAN_BASELINE, 20));
		 showParam.add(f);
		 f_text = new JTextField();
		 f_text.setFont(new Font("宋体", Font.ROMAN_BASELINE, 20));
		 f_text.setBounds(180, 400, 100, 30);
		 showParam.add(f_text);
		 
		 
		 Random_thread = new Thread(new Runnable() {
				@Override
				public void run() {
					while(true){
						try {
							if(state.pausedState==1){
									A_text.setText(RandomUtils.getRandom()+" 度");
									R_text.setText(RandomUtils.getRandom()+" 度");
									status_text.setText(RandomUtils.getRandom()+"");
									E_text.setText(RandomUtils.getRandom()+" m");
									f_text.setText(RandomUtils.getRandom()+" m");
									Thread.sleep(1000);
							}else{
								A_text.setText("");
								R_text.setText("");
								status_text.setText("");
								E_text.setText("");
								f_text.setText("");
							}
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			});
		 Random_thread.start();
		 
		 configTimeArea();
		 
		
		
		JLabel labelMessage = new JLabel("                        接收到的消息");
		labelMessage.setBorder(beRoundBorder);
		labelMessage.setBounds(10, 450, 600, 40);
    	labelMessage.setFont(new Font("宋体", Font.ROMAN_BASELINE, 20));
    	JScrollPane jsc = new JScrollPane(textField);
    	textField.setFont(new Font("宋体", Font.ROMAN_BASELINE, 15));
    	jsc.setBorder(beRoundBorder);
    	jsc.setBounds(10, 500, 600, 100);
    	showParam.add(labelMessage);
    	showParam.add(jsc);
		
		JPanel outp = new JPanel();
		outp.setLocation(704, 30);
		outp.setSize(660, 642);
		outp.setBorder(beRoundBorder);
		outp.setLayout(new BoxLayout(outp, BoxLayout.Y_AXIS));
		outp.add(showParam);
		//outp.add(labelMessage);

		// Plugging the output panel into the main frame
		content.add(outp);

		// Showing the whole window
		setVisible(true);

		// Start Timer
//		Timer timer = new Timer(100, this); // 100 milliseconds
//		timer.start();
		
		//打开监听
		String StringXML = "com/jiaorui/JMSdemo/TopicConsumerXML.xml";
		ApplicationContext context = new ClassPathXmlApplicationContext(StringXML);
	}

	public void actionPerformed(ActionEvent e) {
	}
	
	
	
    public void stateChanged(ChangeEvent e) {
        //canvas.changeColor(colorChooser.getColor());
    }

	public void keyPressed(KeyEvent e) {

	}

	public void keyReleased(KeyEvent e) {
	}

	public void keyTyped(KeyEvent e) {
	}
	
	/**
	 * 接受消息的方法
	 * @param string
	 */
	public static void changeTxt(){
		
			stateButton.doClick();
		
	}
	
	//时间
	private void configTimeArea() {
		 Timer tmr = new Timer();
		 tmr.scheduleAtFixedRate(new JLabelTimerTask(),new Date(), ONE_SECOND);
	}
	protected class JLabelTimerTask extends TimerTask{
		 SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT);
		 @Override
		 public void run() {
		  time = dateFormatter.format(Calendar.getInstance().getTime());
		  s_text.setText(time);
	     }
	 }	
	
	
	
}
